<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
        <el-form-item label="出版服务名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="出版服务名称"></el-input>
      </el-form-item>
      <el-form-item label="出版社英文名称" prop="ename">
          <el-input v-model="dataForm.ename" placeholder="出版社英文名称"></el-input>
      </el-form-item>
          <!-- <el-input v-model="dataForm.image" placeholder="服务封面"></el-input> -->
          <el-form-item label="封面图"  prop="image">
            <el-upload
                ref="upload"
                action=""
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-change="UploadImage"
                :limit="1"
                :file-list="fileList"
                :auto-upload="false"
            >
              <i class="el-icon-plus"></i>
              <template #tip>
                <div style="font-size: 12px;color: #919191;">
                  单次限制上传一张图片
                </div>
              </template>
            </el-upload>
            <el-dialog v-model="dialogVisible" style="line-height: 0;">
              <img style="width: 100%;height: 100%"  :src="dialogImageUrl" alt="" />
            </el-dialog>
      </el-form-item>
      
          <el-form-item label="详情" prop="detail">
          <el-input v-model="dataForm.detail" placeholder="详情"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        name: '',
        id: '',
        image: '',
        detail: '',
        ename:'',
      },
      fileList:[],
      dialogImageUrl: '',
      dialogVisible: false,
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        image: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        detail: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ename: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 关于获取图片
    //关于上传图片到本地的函数
    //上传图片的方法
    UploadImage(file,filelist) {
      let fd = new FormData()
      fd.append('file', file.raw) //传给后台接收的名字 file
      this.$http.post('/bookManagement/books/upload',fd, {headers: {'Content-Type': 'multipart/form-data'}}).then(res=>{
          //上传成功后返回的数据,
        console.log("上传图片到:"+res.data);
        // 将图片地址给到表单.
        this.dataForm.image=res.data
      })
 
    },
    //移除图片功能
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },

    //预览图片功能
    handlePictureCardPreview(file) {
      console.log("这是预览地址"+file.url);
      this.dialogVisible = true
      this.dialogImageUrl = file.url
    },

    //上传图片到本地的函数
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/bookManagement/pressservice/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/bookManagement/pressservice/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
